// $Id$
// Licensed under Apache License Version 2.0
// See: http://www.apache.org/licenses/LICENSE-2.0.html
package de.servicezombie.tcmdb.rest.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;

import de.servicezombie.cxf.WebserviceTemplate;
import de.servicezombie.cxf.WebserviceTemplateFactory;
import de.servicezombie.pub.r1.CommonRestResponse;
import de.servicezombie.tcmdb.dao.mongo.SimpleCrudDao;

abstract class RestEndpointTestUtils {
	
	private static final Logger log = LoggerFactory.getLogger(RestEndpointTestUtils.class);

	public static void assertRecords(SimpleCrudDao<?> dao, int expectedRecordCount) {
		Assert.assertEquals(dao.findAll().size(), expectedRecordCount);
	}

	public static <T> CommonRestResponse<T> getWebserviceResponse() {
		WebserviceTemplate template = new WebserviceTemplateFactory().build();
		CommonRestResponse<T> crr = template.getWebserviceResponse();
		return crr;
	}

	public static InputStream asXml(Object object) throws JAXBException {

		JAXBContext jaxb = JAXBContext.newInstance(CommonRestResponse.class, object.getClass());
		StringWriter out = new StringWriter();
		jaxb.createMarshaller().marshal(object, out);

		if (log.isDebugEnabled()) {
			log.debug("asXml: << \n" + out);
		}
		return new ByteArrayInputStream(out.toString().getBytes());
	}

}
